<?php
	$title='Manage Site';
	$description='manage';
	$keywords='manage';
?>
<?php include('fns_db_manage_users.php'); ?>
<?php include('../views/layouts/_header.php'); ?>
<?php include('../views/layouts/_left.php'); ?>
<?php
if(!checkIfAdmin()){
	header("location: ../index.php");
}
?>



<?php
function showAllUsersInfo(){
	$result = get_all_users_ordered_by_date();

	echo 'List of all users:<br/><br/>';

	printf ("<table border='1'><tr><th>User Name</th><th>Rank</th><th>Create Date</th><th>First Name</th><th>Last Name</th><th>City</th><th>Country</th><th>Email</th><th>Manage</th></tr>");

	$myrow = mysqli_fetch_assoc ($result);
	do {
		printf ("<tr>
		             <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td><a href='manage_users.php?action=changeRank&id=%s'>Change Rank</a></td>", $myrow["user_name"], $myrow["rank"], $myrow["create_date"], $myrow["first_name"],$myrow["last_name"],  $myrow["city"], $myrow["country"], $myrow["email"], $myrow["id"]);



		print("</tr>");

	}while ($myrow = mysqli_fetch_assoc ($result));

	print("</table>");
}

function showSingleUserInfo($id){
	$result = get_single_user($id);

	echo '<form action="manage_users.php?action=changeRank&rank=changed&id='.$id.'" method="post">';
	printf ("<table border='1'><tr><th>User Name</th><th>Rank</th><th>Create Date</th><th>First Name</th><th>Last Name</th><th>City</th><th>Country</th><th>Email</th></tr>");

	$myrow = mysqli_fetch_assoc ($result);
	do {
		printf ("<tr>
		             <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td>", $myrow["user_name"], $myrow["rank"], $myrow["create_date"], $myrow["first_name"],$myrow["last_name"],  $myrow["city"], $myrow["country"], $myrow["email"]);



		print("</tr>");

	}while ($myrow = mysqli_fetch_assoc ($result));

	print("</table>");

	echo '<br/>Set new rank:<br/> ';
	echo "<input name='new_rank' type='radio' value='newbie'>newbie<br/>";
	echo "<input name='new_rank' type='radio' value='apprentice'>apprentice<br/>";
	echo "<input name='new_rank' type='radio' value='master'>master<br/>";

	echo '<br/><input name="submit" type="submit" value="Save">';
	echo '</form>';
}


if(isset($_GET['action']) && isset($_GET['rank'])){
	//change rank
	if(isset($_POST['new_rank'])){
		change_user_rank($_GET['id'], $_POST['new_rank']);
	}
	showAllUsersInfo();
}else if(isset($_GET['action'])){
	//show form to change user's rank
	showSingleUserInfo($_GET['id']);

}else{
	showAllUsersInfo();
}

?>


<?php include('../views/layouts/_footer.php'); ?>